package com.ant.collect.controller;


import com.ant.collect.common.ResultPage;
import com.ant.collect.common.ResultVo;
import com.ant.collect.dto.ExportDto;
import com.ant.collect.handler.AntException;
import com.ant.collect.dto.CollectionYearDto;
import com.ant.collect.dto.ProductDto;
import com.ant.collect.service.ICollectionYearService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hzd
 * @since 2025-10-25
 */
@Api(tags = "行业接口")
@RestController
@RequestMapping("/product")
public class CollectionYearController {

    @Autowired
    ICollectionYearService yearService;

    @ApiOperation(value = "获取所有行业列表")
    @GetMapping("/list")
    public ResultVo list(){
        return ResultVo.success("ok",yearService.list());
    }


    @ApiOperation(value = "获取通过id获取列表")
    @ApiImplicitParam(name = "id", value = "企业id", required = true,dataType = "Integer", paramType = "query")
    @GetMapping("/findProductDetails")
    public ResultVo findProductDetails(Integer id){
        if(id==null){
            throw new AntException("请输入企业id");
        }
        return ResultVo.success("ok",yearService.findProductDetails(id));
    }


    @ApiOperation(value = "获取通过企业id获取列表")
    @ApiImplicitParam(name = "id", value = "企业id", required = true,dataType = "Integer", paramType = "query")
    @GetMapping("/listById")
    public ResultVo listById(Integer id){
        if(id==null){
            throw new AntException("请输入企业id");
        }
        return ResultVo.success("ok",yearService.listById(id));
    }

    @ApiOperation(value = "保存行业信息")
    @PostMapping("/save")
    public ResultVo save(@RequestBody CollectionYearDto dto)
    {
        return ResultVo.success("添加成功",yearService.saveCollectionYear(dto));
    }

    @ApiOperation(value = "获取采集分页信息")
    @GetMapping("/listPage")
    public ResultPage listPage(ProductDto dto)

    {
        return ResultPage.success("查询成功",yearService.listPage(dto));
    }

    //导出
    @ApiOperation(value = "导出")
    @GetMapping("/export")
    public void export(ExportDto dto){

        // 1. 准备导出数据（按公司排序）
//        List<CompanyExportDTO> exportData = buildExportData();
//
//        // 2. 设置响应头
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("UTF-8");
//        String fileName = URLEncoder.encode("带标题样式的公司数据", "UTF-8").replaceAll("\\+", "%20");
//        response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + fileName + ".xlsx");
//
//        // 3. 写入Excel，同时应用：标题样式 + 公司列合并策略
//        EasyExcel.write(response.getOutputStream(), CompanyExportDTO.class)
//                .registerWriteHandler(ExcelStyleUtils.getTitleStyleStrategy()) // 标题样式
//                .registerWriteHandler(new CompanyMergeStrategy(exportData)) // 公司列合并
//                .sheet("数据")
//                .doWrite(exportData);
        yearService.export(dto);
    }



}
